//
//  CLUserDefaults.swift
//  CJBookReader
//
//  Created by colin on 2019/3/21.
//  Copyright © 2019 lidi. All rights reserved.
//

import UIKit

class CLUserDefaults: NSObject {

    static let shared = CLUserDefaults()
    
    // MARK: -- 清理 NSUserDefaults
    
    /// 清空 NSUserDefaults
    func clear() {
        let defaults = UserDefaults.standard
        for key in defaults.dictionaryRepresentation().keys {
            defaults.removeObject(forKey: key)
            defaults.synchronize()
        }
    }
    
    /// 删除 对应Key 的值
    func remove(key:String) {
        let defaults = UserDefaults.standard
        defaults.removeObject(forKey: key)
        defaults.synchronize()
    }
    
    // MARK: -- 存
    
    /// 存储Object
    func set(object value:Any?,key:String) {
        let defaults = UserDefaults.standard
        defaults.set(value, forKey: key)
        defaults.synchronize()
    }
    
    /// 存储String
    func set(string value:String?,key:String) {
        self.set(object: value, key: key)
    }
    
    /// 存储Int
    func set(int value:Int,key:String) {
        let defaults = UserDefaults.standard
        defaults.set(value, forKey: key)
        defaults.synchronize()
    }
    
    /// 存储Bool
    func set(bool value:Bool,key:String) {
        let defaults = UserDefaults.standard
        defaults.set(value, forKey: key)
        defaults.synchronize()
    }
    
    /// 存储Float
    func set(float value:Float,key:String) {
        let defaults = UserDefaults.standard
        defaults.set(value, forKey: key)
        defaults.synchronize()
    }
    
    /// 存储Double
    func set(double value:Double,key:String) {
        let defaults = UserDefaults.standard
        defaults.set(value, forKey: key)
        defaults.synchronize()
    }
    
    /// 存储URL
    func set(url value:URL?,key:String) {
        let defaults = UserDefaults.standard
        defaults.set(value, forKey: key)
        defaults.synchronize()
    }
    
    
    // MARK: -- 取
    
    /// 获取Object
    func object(key:String) -> Any? {
        let defaults = UserDefaults.standard
        return defaults.object(forKey: key)
    }
    
    /// 获取String
    func string(key:String) -> String {
        let defaults = UserDefaults.standard
        let string = defaults.object(forKey: key) as? String
        return string ?? ""
    }
    
    /// 获取Bool
    func bool(key:String) -> Bool {
        let defaults = UserDefaults.standard
        return defaults.bool(forKey: key)
    }
    
    /// 获取NSInteger
    func int(key:String) -> NSInteger {
        let defaults = UserDefaults.standard
        return defaults.integer(forKey: key)
    }
    
    /// 获取Float
    func float(key:String) -> Float {
        let defaults = UserDefaults.standard
        return defaults.float(forKey: key)
    }
    
    /// 获取Double
    func double(key:String) -> Double {
        let defaults = UserDefaults.standard
        return defaults.double(forKey: key)
    }
    
    /// 获取URL
    func url(key:String) -> URL? {
        let defaults = UserDefaults.standard
        return defaults.url(forKey: key)
    }
    

}
